import { WL_SessionInfo, WL_UserInfo } from "../proto/weilapb_data_define";
import { WeilaStorageInterface, WL_LoginParam } from "./weila_datas";

class WeilaStore {
    private loginUserInfo: WL_UserInfo | null = null;
    private loginToken: string | null = null;
    private msgSeq: any;
    private loginParam: WL_LoginParam | null = null;
    private appId: string | null;
    private appKey: string | null;
    private websocketAddr: string | null;
    private apiBaseUrl: string | null;
    private static instance: WeilaStore | null;
    private muteSession: WL_SessionInfo[] = [];
    private enviourment: string | null = null;

    private storageImpl: WeilaStorageInterface | null = null;

    public static getInstance(): WeilaStore {
        if (!this.instance) {
            this.instance = new WeilaStore();
        }
        return this.instance;
    }

    public setStorageImpl(storageImpl: WeilaStorageInterface) {
        this.storageImpl = storageImpl;
    }

    constructor() {
        this.appId = import.meta.env.VITE_APPID || "";
        this.appKey = import.meta.env.VITE_APPKEY || "";
        this.websocketAddr = import.meta.env.VITE_WEBSOCKET_ADDR || "";
        this.apiBaseUrl = import.meta.env.VITE_API_BASE_URL || "";

        console.log(this.appId, this.appKey, this.websocketAddr, this.apiBaseUrl);
        
    }
    public setAppId(appId: string) {
        this.appId = appId;
        this.storageImpl?.setItem("appId", appId);
    }

    public setAppKey(appKey: string) {
        this.appKey = appKey;
        this.storageImpl?.setItem("appKey", appKey);
    }
    
    public setWebsocketAddr(websocketAddr: string) {
        this.websocketAddr = websocketAddr;
        this.storageImpl?.setItem("websocketAddr", websocketAddr);
    }

    public setApiBaseUrl(apiBaseUrl: string) {
        this.apiBaseUrl = apiBaseUrl;
        this.storageImpl?.setItem("apiBaseUrl", apiBaseUrl);
    }
    

    public getAppId(): string | null {
        return this.appId;
    }

    public getAppKey(): string | null {
        return this.appKey;
    }

    public getWebsocketAddr(): string | null {
        return this.websocketAddr;
    }

    public getApiBaseUrl(): string | null {
        return this.apiBaseUrl;
    }
    

    public setLoginParam(loginParam: WL_LoginParam | null) {
        this.loginParam = loginParam;
        if (loginParam) {
            this.storageImpl?.setItem("loginParam", JSON.stringify(loginParam));
        }else {
            this.storageImpl?.removeItem("loginParam");
        }
    }

    public getLoginParam(): WL_LoginParam | null {
        const data = this.storageImpl?.getItem("loginParam");
        if (data) {
            this.loginParam = JSON.parse(data) as WL_LoginParam;
        }
        return this.loginParam;
    }
    
    public setLoginUserInfo(loginUserInfo: WL_UserInfo) {
        this.loginUserInfo = loginUserInfo;
    }

    public getLoginUserInfo(): WL_UserInfo | null {
        return this.loginUserInfo;
    }

    public setLoginToken(loginToken: string) {
        this.loginToken = loginToken;
        this.storageImpl?.setItem("loginToken", loginToken);
    }

    public getLoginToken(): string | null {
        return this.loginToken;
    }

    public setMsgSeq(msgSeq: number) {
        this.msgSeq = msgSeq;
        this.storageImpl?.setItem("msgSeq", msgSeq.toString());
    }

    public getMsgSeq(): number {
        const data = this.storageImpl?.getItem("msgSeq");
        if (data) {
            this.msgSeq = parseInt(data);
        }
        return this.msgSeq;
    }

    public setEnviourment(enviourment: string) {
        this.enviourment = enviourment;
        this.storageImpl?.setItem("enviourment", enviourment);
    }

    public getEnviourment(): string | null {
        return this.enviourment;
    }
}

export { WeilaStore };